package cn.tedu.fy.pethome.service.impl;

import cn.tedu.fy.pethome.ex.ServiceException;
import cn.tedu.fy.pethome.mapper.GoodsMapper;
import cn.tedu.fy.pethome.pojo.dto.AdminSuppliesAddNewDTO;

import cn.tedu.fy.pethome.pojo.dto.SuppliesAddNewDTO;
import cn.tedu.fy.pethome.pojo.entity.Goods;
import cn.tedu.fy.pethome.pojo.vo.GoodsDetailVO;
import cn.tedu.fy.pethome.service.IAdminSuppliesService;
import cn.tedu.fy.pethome.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 添加宠物用品业务的实现类
 *
 * @author KJ2476
 * @date 2022/7/23
 **/
@Slf4j
@Service
public class AdminSuppliesServiceImpl implements IAdminSuppliesService {
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 添加宠物用品
     *
     * @param adminSuppliesAddNewDTO 待添加的宠物用品对象
     */
    @Override
    public void insert(AdminSuppliesAddNewDTO adminSuppliesAddNewDTO) {
        log.debug("开始处理管理员添加宠物用品的业务，接受到参数为：{}", adminSuppliesAddNewDTO);
        String name = adminSuppliesAddNewDTO.getGoodsname();
        GoodsDetailVO byName = goodsMapper.getByName(name);
        if (byName != null) {
            String msg = "该宠物用品的商品名已存在";
            log.debug("{},它的id是{}", msg, byName.getId());
            throw new ServiceException(ServiceCode.ERR_CONFLICT, msg);
        }
        Goods goods = new Goods();
        BeanUtils.copyProperties(adminSuppliesAddNewDTO, goods);

        // 补全属性
//        goods.setPetCategoryId(0L);
//        goods.setPetCategoryName("无");
        goods.setSales(0);
        goods.setCommentCount(0);
        goods.setPositiveCommentCount(0);
        goods.setIsDeleted(0);
        goods.setIsRecommend(1);
        goods.setIsChecked(0);
        goods.setCheckUser("无");

        int rows = goodsMapper.insert(goods);
        if (rows != 1) {
            String msg = "添加失败,服务器忙,请稍后再试";
            log.debug("插入失败, 插入结果不为1, 而是{}",rows);
            throw new ServiceException(ServiceCode.ERR_INSERT, msg);
        }
    }

}
